/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Servlets;

import Controleurs.InterfaceControleur;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author CDI102
 */
public class ControleurPrincipal extends HttpServlet {
HashMap<String, InterfaceControleur> hmapCtrl;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
              hmapCtrl = new HashMap<String, InterfaceControleur>();

        for (Enumeration<String> e = config.getInitParameterNames(); e.hasMoreElements();) {
            String name = e.nextElement();
            if (name.endsWith(".cmd")) {
                String value = this.getInitParameter(name);

                try {
                    InterfaceControleur sousControleur = (InterfaceControleur) Class.forName(value).newInstance();
                    hmapCtrl.put(name, sousControleur);

                } catch (InstantiationException ex) {
                    Logger.getLogger(ControleurPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IllegalAccessException ex) {
                    Logger.getLogger(ControleurPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ControleurPrincipal.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String url = "/WEB-INF/accueil.jsp";
        String section = request.getParameter("section");       
    
        if (section != null) {
            if (hmapCtrl.containsKey(section)) {
                InterfaceControleur ctrl = hmapCtrl.get(section);
                url = ctrl.execute(request, response);
            }
        }

            
            request.getRequestDispatcher(url).include(request, response);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
